<?php	include_once('inc/header.php');
		include_once('inc/product.inc.php'); ?>
	

	<!--page-->
	<div id="banner">
		<div class="container intro_wrapper">
			<div class="inner_content">
			
		<?php	
				if(!empty($_SESSION['cart']['pr_id']))
				{
					echo '<h2 style="text-transform: uppercase;">Filled cart</h2>
					
					<h1 class="title" style="text-transform: uppercase;" id="name">My cart</h1>
					<h3>
						<div id="desc">Don\'t forget to use <span class="hue">coupons</span> if you own some!
						</div>
					</h3>';
				}
				else echo'<h2 style="text-transform: uppercase;">Empty cart</h2>
				
				<h1 class="title" style="text-transform: uppercase;" id="name">My cart</h1>
					<h3>
						<div id="desc">You have not added any <span>product</span> in your <span class="hue">cart</span> for the now. Visit the
						<span><a href = "browse_products.php">product\'s browser</a></span> to discover all the bargains available in <span>your city</span>!
						</div>
					</h3>';
				
				echo '
				</div>
			</div>
		</div>
	<div class="container wrapper" id="containerWrapper">
		<div class="inner_content">';
		
	// If the user has asked for remove a product from his cart
	if(isset($_GET['remove']) && check_product($db, htmlspecialchars($_GET['remove'])))
	{
		actionCart("removeFromCart", htmlspecialchars($_GET['remove']));
	}
	
	// If the user has modify the quantity of some product in his cart
	if(isset($_POST['action']) && $_POST['action'] == "modifQty")
	{
		$length = count($_SESSION['cart']['pr_id']);
		$pr_id = check_product($db, htmlspecialchars($_POST['pr_id']));
		$quantity = htmlspecialchars($_POST['quantity']);
		$toRemove = false;
		
		if($pr_id)
		{
			for($i=0; $i<$length; $i++)
			{
				if($_SESSION['cart']['pr_id'][$i] == $pr_id)
				{
					$_SESSION['cart']['nb_item'][$i] = $quantity;
					if(!$quantity)
					{
						$toRemove = true;
					}
				}
			}
			if($toRemove)
			{
				actionCart("removeFromCart", $pr_id);
			}
		}
	}
	
	/* If the cart is filled */
	if(!empty($_SESSION['cart']['pr_id']))
	{	
		$nbProducts = count($_SESSION['cart']['pr_id']);  //Quantity of products belonging to the cart
		$Products = array(); // Array containing the instances of the cart's products
		$cPage = 1; // Current page
		$perPage = 5; // Quantity of displayed products per page
		$nbPages = ceil($nbProducts/$perPage); // Quantity of all pages
		
		// We try to retrieve the current page if it has change
		if (isset($_POST['page']) && ($_POST['page'])>0 && ($_POST['page'])<= $nbPages)
		{
			$cPage = htmlspecialchars($_POST['page']);
		}
		
		// We get the products
		$req = $db->prepare('SELECT pr_id FROM jktrade_products WHERE pr_id = :pr_id');
		
		for($i=0; $i < $nbProducts; $i++)
		{
			if(isAvailable($db, $_SESSION['cart']['pr_id'][$i]))
			{
				$req->bindParam('pr_id', $_SESSION['cart']['pr_id'][$i], PDO::PARAM_INT);
				$req->execute();
				$data = $req->fetch(PDO::FETCH_ASSOC);
				
				$Products[] = new Product($db, $data['pr_id']);
			}
		}
		
		$firstProduct = ($cPage-1)*$perPage;
		
		echo' <table class="table table-striped">
                     <thead>
                         <tr>
                            <th></th>
                            <th>Product\'s name</th>
                            <th>Quantity</th>
                            <th>Price</th>
							<th>Remove</th>
                        </tr>
                    </thead>
                    <tbody>
		';
						
		//Let display them!
		for($i=$firstProduct; $i < ($firstProduct+$perPage); $i++)
		{		
			if(isset($Products[$i]))
			{
				echo'
							  <tr>
								<td><div class="hover_img" style="max-width:200px"><a href="show_product.php?pr='.($Products[$i]->id).'">
						<img src="img/uploads/products/thumb/'.($Products[$i]->mainpic).'" alt="img'.($Products[$i]->id).'"/></a>
					</div></td>
								<td style="vertical-align:middle">'.($Products[$i]->name).'</td>
								<td style="vertical-align:middle"><input id="'.$Products[$i]->id.'" type="number" min="0" style="max-width:50px;" value="'.$_SESSION['cart']['nb_item'][$i].'"></td>
								<td style="vertical-align:middle">'.$Products[$i]->price*$_SESSION['cart']['nb_item'][$i].' KR</td>
								<td style="vertical-align:middle"><a class="remove" href="cart.php?remove='.($Products[$i]->id).'"><button type="button" class="btn btn-warning">Yes <i class="fa fa-trash-o icon-large"></i></button></a></td>
							  </tr>
				';
			}
		}
		
		echo '</tbody>
                      </table>
		
		 <table class="table table-striped">
			 <thead>
				 <tr>
					<th></th>
					<th>Quantity</th>
					<th>Price</th>
					<th>Remove All</th>
					<th>Proceed to Checkout</th>
				</tr>
			</thead>
				
			<tbody>	
				<tr>
					<th style="vertical-align:middle">Total</th>
					<td style="vertical-align:middle">'.getInfosCart($Products, "itemsQuantity").'</td>
					<td style="vertical-align:middle">'.getInfosCart($Products, "itemsPrice").' KR</td>
					<td style="vertical-align:middle"><a class="remove" id="removeAll" href="cart.php?remove=All"><button style="padding-right: 40px;padding-left: 40px;" type="button" class="btn btn-danger">Yes <i class="fa fa-trash-o icon-large"></i></button></a></td>
					<td style="vertical-align:middle"><a id="checkoutGo" href="checkout.php?op=proceed"><button style="padding-right: 40px;padding-left: 40px;" type="button" class="btn btn-success">Yes <i class="fa fa-check-square-o icon-large"></i></button></a></td>
				</tr>
		
			</tbody>
        </table>';
		 
		 /* Displaying the pagination system */
		
		echo '<!-- //portfolio_block -->   
		<div class="holder">
		
			<a class="page" id="1" href="cart.php">← first</a>';
			
			if($cPage>2)
			{
				echo '<span style="margin-right:8px" class="jp-hidden">...</span>';
			}
			
			if($cPage>1)
			{
				echo '<a class="page" id="'.($cPage-1).'" href="cart.php">'.($cPage-1).'</a>';
			}
			
			echo '<a id="'.($cPage).'" href="cart.php" class="jp-current">'.($cPage).'</a>';
			
			if($cPage < $nbPages)
			{
				echo '<a class="page" id="'.($cPage+1).'" href="cart.php">'.($cPage+1).'</a>';
			}
			
			if($cPage<($nbPages-1))
			{
				echo '<span style="margin-right:8px" class="jp-hidden">...</span>';
			}
			echo'<a class="page" id="'.$nbPages.'" href="cart.php">last →</a>
		</div>';
		
	}
	else
	{
		echo'</br></br><legend>Let\'s do some shopping! <i class="fa fa-money icon-large"></i></legend>';
	}
	
	echo'	</div>
		</div>
		<div class="pad25"></div>';
	
			
include_once('inc/footer.php'); ?>